/**
 * @author tian qingsen
 */
$(function() {
	
	$('#login_btn').click(function(e) {
		var userName = $('#user_name').val();
		var userPwd = $('#user_pwd').val();
		var captchaResponse = $('#captcha_response').val();
		var data = {
			'userName' : encodeURIComponent(userName),
			'userPwd' : encodeURIComponent(userPwd),
			'captchaResponse' : captchaResponse
		};
		
		$('#feedback_message').text('正在登陆...');
		
		var url = window.ctx + '/admin/checkLogin.htm';
		$.getJSON(url, data, function(feedback) {
			if (feedback.successful) {
				window.location.href = window.ctx + '/admin/manage.htm';
			} else {
				$('#feedback_message').text(feedback.message);
				if (feedback.type && feedback.type == 'captcha') {
					$('#captcha_response').focus().select();
				}
				refreshCaptcha();
			}
		});
	});
	
	$('#captcha_img').click(function(e) {
		refreshCaptcha();
		return false;
	});
	/**
	 * 刷新captcha的验证图片.
	 */
	function refreshCaptcha() {
		$('#captcha_img').hide().attr('src',window.ctx + '/security/jcaptcha?'+ Math.floor(Math.random() * 100)).fadeIn();
	}
});
